// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "PMASSParser.h"

#include <algorithm>
#include <cstdint>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Property/IPMASSService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (PMASS);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/PMASS/PMASS.md>
*/

// 定义PMASS关键字的枚举，表示每个字段的位置索引
enum PMASSKey : std::uint8_t
{
    PMASSKEY_PID1 = 2,
    PMASSKEY_M1   = 3,
    PMASSKEY_PID2 = 4,
    PMASSKEY_M2   = 5,
    PMASSKEY_PID3 = 6,
    PMASSKEY_M3   = 7,
    PMASSKEY_PID4 = 8,
    PMASSKEY_M4   = 9,
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParsePMASS (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                             std::vector<std::string>&                               dividedEntryInfo)
{
    // 初始化临时的PMASS数据结构，用于存储解析后的数据
    SG::DataStructure::FEM::PMASSData tempPMASS1;
    SG::DataStructure::FEM::PMASSData tempPMASS2;
    SG::DataStructure::FEM::PMASSData tempPMASS3;
    SG::DataStructure::FEM::PMASSData tempPMASS4;

    // TODO(bug 001) m_domainId的正确填写，目前默认设置为1
    tempPMASS1.m_domainId = tempPMASS2.m_domainId = tempPMASS3.m_domainId = tempPMASS4.m_domainId = 1;

    // 分割输入字符串EntryInfo，并将其结果存储到dividedEntryInfo
    bool isHaveSecond = false;
    bool isHaveThird  = false;
    bool isHaveForth  = false;

    int curIDKey = 1;  // 当前处理的字段索引

    // 遍历分割后的字符串条目
    for (auto curStrKey : dividedEntryInfo)
    {
        // 检查当前字符串是否为空（只包含空格）
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;  // 跳过空字段，继续下一个字段
            continue;
        }
        // 将字符串转换为科学计数法

        // 根据当前字段索引解析相应的数据并存储到tempPMASS
        switch (curIDKey)
        {
        case PMASSKEY_PID1:
            tempPMASS1.m_id = convertToI64 (curStrKey);
            break;
        case PMASSKEY_M1:
            tempPMASS1.m_M = convertToReal (curStrKey);
            break;
        case PMASSKEY_PID2:
            isHaveSecond    = true;
            tempPMASS2.m_id = convertToI64 (curStrKey);
            break;
        case PMASSKEY_M2:
            tempPMASS2.m_M = convertToReal (curStrKey);
            break;
        case PMASSKEY_PID3:
            isHaveThird     = true;
            tempPMASS3.m_id = convertToI64 (curStrKey);
            break;
        case PMASSKEY_M3:
            tempPMASS3.m_M = convertToReal (curStrKey);
            break;
        case PMASSKEY_PID4:
            isHaveForth     = true;
            tempPMASS4.m_id = convertToI64 (curStrKey);
            break;
        case PMASSKEY_M4:
            tempPMASS4.m_M = convertToReal (curStrKey);
            break;
        default:
            // 如果索引不匹配任何已知字段，跳过
            break;
        }

        curIDKey++;  // 处理完当前字段，移动到下一个字段
    }

    if (isHaveSecond)
    {
        dbServiceFactory->get<IPMASSService> ()->Add (tempPMASS2);
    }
    if (isHaveThird)
    {
        dbServiceFactory->get<IPMASSService> ()->Add (tempPMASS3);
    }
    if (isHaveForth)
    {
        dbServiceFactory->get<IPMASSService> ()->Add (tempPMASS4);
    }
    // 将解析后的PMASS数据添加到数据库服务中
    return dbServiceFactory->get<IPMASSService> ()->Add (tempPMASS1);
}